/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.inventory.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.suli.inventory.domain.TallyTasks;
import me.zhengjie.suli.inventory.service.TallyTasksService;
import me.zhengjie.suli.inventory.service.dto.TallyTasksQueryCriteria;
import me.zhengjie.suli.wcs.service.impl.AgvTasksServiceImpl;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author Allen Gao
* @date 2024-05-22
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "理货管理")
@RequestMapping("/api/tallyTasks")
public class TallyTasksController {

    private final TallyTasksService tallyTasksService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('tallyTasks:list')")
    public void exportTallyTasks(HttpServletResponse response, TallyTasksQueryCriteria criteria) throws IOException {
        tallyTasksService.download(tallyTasksService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询理货")
    @ApiOperation("查询理货")
    @PreAuthorize("@el.check('tallyTasks:list')")
    public ResponseEntity<Object> queryTallyTasks(TallyTasksQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(tallyTasksService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增理货")
    @ApiOperation("新增理货")
    @PreAuthorize("@el.check('tallyTasks:add')")
    public ResponseEntity<Object> createTallyTasks(@Validated @RequestBody TallyTasks resources){
        return new ResponseEntity<>(tallyTasksService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改理货")
    @ApiOperation("修改理货")
    @PreAuthorize("@el.check('tallyTasks:edit')")
    public ResponseEntity<Object> updateTallyTasks(@Validated @RequestBody TallyTasks resources){
        tallyTasksService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除理货")
    @ApiOperation("删除理货")
    @PreAuthorize("@el.check('tallyTasks:del')")
    public ResponseEntity<Object> deleteTallyTasks(@RequestBody String[] ids) {
        tallyTasksService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping(value = "/continueTallyTasks")
    @Log("继续理货任务")
    @ApiOperation("继续理货任务")
    public ResponseEntity<Object> continueTallyTasks() {
        return new ResponseEntity<>(tallyTasksService.continueTallyTasks(), HttpStatus.OK);
    }

    @PostMapping(value = "/stop")
    @Log("停止理货")
    @ApiOperation("停止理货")
    public ResponseEntity<Object> stop() {
        AgvTasksServiceImpl.flag = true;
        return new ResponseEntity<>(1, HttpStatus.OK);
    }
}